//
//  RecentlyChatListViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#pragma mark --------- 聊天列表

#import "RecentlyChatListViewController.h"
#import "ChatlistCell.h"
#import <TUIConversationListController.h>
#import "ChatViewController.h"
#import <TPopView.h>
#import <TPopCell.h>
#import "FriendListViewController.h"
#import "ScanQRCodeViewController.h"
#import "AddFriendViewController.h"
#import "AddGroupChatViewController.h"
#import "GroupListViewController.h"

@interface RecentlyChatListViewController ()<TUIConversationListControllerDelegagte,TPopViewDelegate>
@property (nonatomic, strong) TNaviBarIndicatorView *titleView;

@end

@implementation RecentlyChatListViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"豚友";
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    self.titleView = [[TNaviBarIndicatorView alloc] init];
    [self.titleView setTitle:@"豚集"];
    self.navigationItem.titleView = self.titleView;
    self.navigationItem.title = @"";
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [moreButton setImage:[UIImage imageNamed:@"discountcoupon_more"] forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(rightBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *moreItem = [[UIBarButtonItem alloc] initWithCustomView:moreButton];
    self.navigationItem.rightBarButtonItem = moreItem;
}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    TUIConversationListController *conv = [[TUIConversationListController alloc] init];
    conv.delegate = self;
    [self addChildViewController:conv];
    [self.view addSubview:conv.view];
}

- (void) initLayout {
    
}

- (void) initAction {
    //监听网络状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNetworkChanged:) name:TUIKitNotification_TIMConnListener object:nil];
}

- (void)initData {

}

#pragma mark - delegate
-(void)conversationListController:(TUIConversationListController *)conversationController didSelectConversation:(TUIConversationCell *)conversation {
    ChatViewController *chat = [[ChatViewController alloc] init];
    chat.conversation = conversation.convData;
    
    [self.navigationController pushViewController:chat animated:YES];
}

- (void)popView:(TPopView *)popView didSelectRowAtIndex:(NSInteger)index {
    switch (index) {
        case 0://通讯录
        {
            [self.navigationController pushViewController:[[FriendListViewController alloc] init] animated:YES];
        }
            break;
        case 1://添加好友
        {
            [self.navigationController pushViewController:[[AddFriendViewController alloc] init] animated:YES];

        }
            break;
        case 2://发起聊天
        {
            [self.navigationController pushViewController:[[FriendListViewController alloc] init] animated:YES];
        }
            break;
        case 3://发起群聊
        {
            AddGroupChatViewController *vc = [[AddGroupChatViewController alloc] init];
            vc.groupMemberHandelType = GroupMemberHandelTypeCreate;
            [self.navigationController pushViewController:vc animated:YES];

        }
            break;
        case 4://我的群聊
        {
            GroupListViewController *vc = [[GroupListViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 5://扫一扫
        {
            [self.navigationController pushViewController:[[ScanQRCodeViewController alloc] init] animated:YES];
        }
            break;
        case 6://我的二维码
            
            break;
        default:
            break;
    }
}
#pragma mark - target action
- (void)rightBarButtonClick:(UIButton *)rightBarButton {
    NSMutableArray *menus = [NSMutableArray array];
    
    NSArray *iconArr = @[@"chat_addressbook",
                         @"chat_addfriend",
                         @"chat_addchat",
                         @"chat_addgroupchat",
                         @"chat_groupchat",
                         @"chat_scanqrcode",
                         @"chat_qrcode"];
    NSArray *titleArr = @[@"通讯录",
                          @"添加豚友",
                          @"发起聊天",
                          @"发起群聊",
                          @"我的群聊",
                          @"扫一扫",
                          @"我的二维码"];

    for (int i = 0; i < iconArr.count; i++) {
        TPopCellData *pop = [[TPopCellData alloc] init];
        pop.image = iconArr[i];
        pop.title = titleArr[i];
        [menus addObject:pop];
    }
    
    CGFloat height = [TPopCell getHeight] * menus.count + 10;
    CGFloat orginY = TopBarHeight;
    TPopView *popView = [[TPopView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 140, orginY, 130 * [JTool getScreenWidthScale], height)];
    CGRect frameInNaviView = [self.navigationController.view convertRect:rightBarButton.frame fromView:rightBarButton.superview];
    popView.arrowPoint = CGPointMake(frameInNaviView.origin.x + frameInNaviView.size.width * 0.5, orginY);
    popView.delegate = self;
    [popView setData:menus];
    [popView showInWindow:self.view.window];
}

- (void)onNetworkChanged:(NSNotification *)notification
{
    TUINetStatus status = (TUINetStatus)[notification.object intValue];
    switch (status) {
        case TNet_Status_Succ:
            [_titleView setTitle:@"豚集"];
            [_titleView stopAnimating];
            break;
        case TNet_Status_Connecting:
            [_titleView setTitle:@"连接中..."];
            [_titleView startAnimating];
            break;
        case TNet_Status_Disconnect:
            [_titleView setTitle:@"豚集(未连接)"];
            [_titleView stopAnimating];
            break;
        case TNet_Status_ConnFailed:
            [_titleView setTitle:@"豚集(未连接)"];
            [_titleView stopAnimating];
            break;
            
        default:
            break;
    }
}
- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - private method

#pragma mark - getter and setter


@end
